package wc.Service.Impl;

import wc.Dao.AdminDao;
import wc.Dao.DaoImpl.AdminDaoImpl;
import wc.Service.AdminService;
import wc.Utils.CreditUtil;
import wc.Utils.CyptoUtil;
import wc.domain.Admin;
import wc.domain.History;
import wc.domain.User;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName:ServiceImpl
 * @Author: 鹿建文
 * @Date: 2022/4/6  - 10:48
 * @Describe:
 */
public class AdminServiceImpl implements AdminService {
    AdminDao adminDao = new AdminDaoImpl();

    @Override
    public Admin login(String account, String password) throws SQLException {
        Admin admin = adminDao.selectByAccount(account);
        if (admin != null) {
            String salt = admin.getSalt();
            String tmp = CyptoUtil.encode(password, salt);
            if (tmp.equals(admin.getPassword())) {
                return admin;
            }
        }
        return null;
    }


    @Override
    public boolean updatePassword(Integer id, String oldpassword, String newpassword) throws SQLException {
        boolean res = false;

        Admin admin = adminDao.selectById(id);
        System.out.println(admin);
        if (admin != null) {
            //获取原始密码
            String oldEncodePassword = admin.getPassword();
            String tmp = admin.getSalt();
            String password0 = CyptoUtil.decode(oldEncodePassword, tmp);
            System.out.println(oldEncodePassword+" "+tmp+"_"+password0+" "+oldpassword);
            if (!password0.equals(oldpassword)) {
                System.out.println(1);
                return res;
            }
            System.out.println(12);
            //生成新的盐值
            String salt = CreditUtil.genSalt();
            //对新密码+新盐值进行加密
            String newEncodePassword = CyptoUtil.encode(newpassword, salt);
            //同步
            boolean updateSaltRes = adminDao.updateSalt(id, salt);
            if (!updateSaltRes) {
                System.out.println("1*");
                return res;
            }
            System.out.println("1**");
            res = adminDao.updatePassword(id, newEncodePassword);
        }
        System.out.println("1***");
        return res;
    }

    @Override
    public boolean addUser(User user) throws SQLException {
        boolean res = adminDao.createUser(user);
        return res;
    }

    @Override
    public List<User> selectAllUsersDetailed() throws SQLException {
        List<User> res = adminDao.selectAllUsersDetailed();

        return res;
    }

    @Override
    public HashMap<Integer, List> selectAllUsersList() throws SQLException {
        HashMap<Integer, List> res = adminDao.selectAllUsersList();
        return res;
    }

    @Override
    public HashMap<Integer, List> selectUserCredits(String name) throws SQLException {
        HashMap<Integer, List> res = adminDao.selectUserCredits(name);
        return res;
    }

    @Override
    public ArrayList<History> selectUserCreditsHistorye(Integer userId) throws SQLException {
        ArrayList<History> res = adminDao.selectUserCreditsHistorye(userId);
        return res;
    }
}
